# -*- coding: utf-8 -*-
import json
from .items import DoubanFilmdetailItem


# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# class ScrapyporjectPipeline(object):
#     # 处理名字 评分 主演
#     def open_spider(self, spider):
#         print("优先级低的管道开启")
#         self.f = open("doubanfilm.txt", "w", encoding="utf-8")
#
#     # 处理结构化数据
#     def process_item(self, item, spider):
#         if isinstance(item, ScrapyporjectItem):
#             data = json.dumps(dict(item), ensure_ascii=False) + "\n"
#             self.f.write(data)
#         return item
#
#     def close_spider(self, spider):
#         self.f.close()


class FilmDetailPipeline(object):
    def open_spider(self, spider):
        print("优先级高的管道开启")
        self.f = open("filmdetail.txt", "w", encoding="utf-8")

        # 处理结构化数据

    def process_item(self, item, spider):
        print('电影详情存储中')
        if isinstance(item, DoubanFilmdetailItem): # 如果item在DoubanFilmdetalItem里则执行
            data = json.dumps(dict(item), ensure_ascii=False) + "\n"
            self.f.write(data)
        return item

    def close_spider(self, spider):
        self.f.close()
